package com.coda.clouddisk.service;

import com.coda.clouddisk.entity.AbsObject;
import com.coda.clouddisk.entity.KesunReturn;
import com.coda.clouddisk.exception.BaseException;
import com.coda.clouddisk.mapper.SuperMapper;
import com.coda.clouddisk.message.MessageContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Data
public abstract class SuperService {


    //实体属性
    private Map<String,Object> models;
    //数据访问层对象
    public abstract SuperMapper getMapper();

    /**
     * 新增
     */
    public KesunReturn add() {
        KesunReturn back = new KesunReturn();
        //执行新增
        SuperMapper Mapper = getMapper();
        int result = Mapper.add(models);
        if (result>0){
            back.setCode(MessageContext.SUCCESS_CODE);
            back.setMsg(MessageContext.SUCCESS_MSG);
        }else {
            back.setCode(MessageContext.ERROR_CODE);
            back.setMsg(MessageContext.ERROR_MSG);
        }
        back.setObj(result);
        return back;
    }
    /**
     * 修改
     */
    public KesunReturn edit(){
        KesunReturn back = new KesunReturn();
        //执行修改
        int result = getMapper().edit(models);
        if (result>0){
            back.setCode(MessageContext.SUCCESS_CODE);
            back.setMsg(MessageContext.SUCCESS_MSG);
        }else {
            back.setCode(MessageContext.ERROR_CODE);
            back.setMsg(MessageContext.ERROR_MSG);
        }
        return back;
    }
    /**
     * 查询单个
     */
    public KesunReturn find(){
        KesunReturn back = new KesunReturn();
        AbsObject absObject = getMapper().find(models);
        if (absObject != null ) {
            back.setCode(MessageContext.SUCCESS_CODE);
            back.setObj(absObject);
        }else {
            back.setCode(MessageContext.ERROR_CODE);
            back.setMsg(MessageContext.ERROR_MSG);
        }
        return back;
    }
    /**
     * 查询所有
     */
    public KesunReturn findAll(Map<String,Object> data){
        KesunReturn back = new KesunReturn();
        ArrayList<AbsObject> all = getMapper().findAll(data);

        if (all != null ) {
            back.setCode(MessageContext.SUCCESS_CODE);
            back.setObj(all);
        }else {
            back.setCode(MessageContext.ERROR_CODE);
            back.setMsg(MessageContext.ERROR_MSG);
        }
        return back;
    }

    /**
     * 分页查询
     */
    public KesunReturn findByPage(Map<String,Object> cons){
        KesunReturn back = new KesunReturn();
        //设置分页参数
        PageHelper.startPage(Integer.parseInt((String) cons.get("pageNum")),Integer.parseInt((String) cons.get("pageSize")));
        //执行查询
        List<Map<String, Object>> byPage = getMapper().findByPage(cons);
        //获取分页信息
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(byPage);
        if (pageInfo != null ) {
            back.setCode(MessageContext.SUCCESS_CODE);
            back.setObj(pageInfo);
        }else {
            back.setCode(MessageContext.ERROR_CODE);
            back.setMsg(MessageContext.ERROR_MSG);
        }
        return back;
    }

    /**
     * 删除
     */
    public KesunReturn delete(ArrayList<String> ids){
        KesunReturn back = new KesunReturn();
        //执行删除
        int result = getMapper().deletes(ids);
        if (result > 0) {
            back.setCode(MessageContext.SUCCESS_CODE);
            back.setMsg(MessageContext.SUCCESS_MSG);
        } else {
            back.setCode(MessageContext.ERROR_CODE);
            back.setMsg(MessageContext.ERROR_MSG);
        }
        return back;
    }

}
